package keeto.com.vn.bms.utils;

import java.sql.Date;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.StringTokenizer;

import android.annotation.SuppressLint;
import android.util.Log;

public class StringUtilities {
	public static String convertPrice(String price) {
		if (price!=null && !price.equals("null") && price.length() > 0) {
			String cleanString = price.toString().replaceAll("[,.]", "");
			double parsed =0;
			try {
				parsed = Double.parseDouble(cleanString.trim());

			} catch (Exception e) {
				// TODO: handle exception
			}

			String formated = NumberFormat.getInstance().format((parsed));
			return formated;
		} else {
			return "";
		}
	}
	@SuppressLint("DefaultLocale") public static String convertNamePeople(String name) {
		String result = "";
		if (name!=null && !name.equals("null") && name.length() > 0) {
			name = name.toLowerCase();
			StringTokenizer tokenizer = new StringTokenizer(name);
			while (tokenizer.hasMoreElements()) {
				String s = tokenizer.nextToken();
				result += String.valueOf(s.charAt(0)).toUpperCase() + s.substring(1) +" ";
			}
			if(!result.equals("")){
				result = result.substring(0, result.length() -1);
			}
		}
		return result;
	}
	
	public static int compareDate(String timeSystem, String timeCompare){ //so sanh 2 moc thoi gian
		int relation = 0;
		SimpleDateFormat format = new SimpleDateFormat("dd-mm-yyyy");
		java.util.Date d1 = null;
		java.util.Date d2 = null;
		try {
			d1 = format.parse(timeSystem);
			d2 = format.parse(timeCompare);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long diff = d2.getTime() - d1.getTime(); 
		if(diff >= 0){
			relation = 2; //timecompare > timeSytem
		} else {
			relation = 1; //timecompare < timeSytem
		}
		return relation;
	}
	public static int compareDate1(String timeSystem, String timeCompare){ //so sanh 2 moc thoi gian
		int relation = 0;
		SimpleDateFormat format = new SimpleDateFormat("dd-mm-yyyy hh:mm:ss");
		java.util.Date d1 = null;
		java.util.Date d2 = null;
		try {
			d1 = format.parse(timeSystem);
			d2 = format.parse(timeCompare);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long diff = d2.getTime() - d1.getTime(); 
		if(diff >= 0){
			relation = 2;//timecompare > timeSytem
		} else {
			relation = 1;//timecompare < timeSytem
		}
		return relation;
	}
	public static String addOneDate(String createDate){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		java.util.Date d1 = null;
		try {
			d1 = format.parse(createDate);
		} catch (Exception e) {
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(d1);
		int nam = cal.get(Calendar.YEAR);
		int thang = cal.get(Calendar.MONTH)+1;
		int ngay = cal.get(Calendar.DATE)+1; // them 1 ngay
		String result = ngay + "-" + thang + "-" + nam + " "
				+ cal.get(Calendar.HOUR_OF_DAY) + ":"
				+ cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
		return result;
	}
	public static String convertFormat(String date) {
		SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
		java.util.Date d1 = null;
		try {
			d1 = format.parse(date);
		} catch (Exception e) {
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(d1);
		int nam = cal.get(Calendar.YEAR);
		int thang = cal.get(Calendar.MONTH)+1;
		int ngay = cal.get(Calendar.DATE);
		String result = nam + "-" + thang + "-" + ngay + " "
				+ cal.get(Calendar.HOUR_OF_DAY) + ":"
				+ cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
		return result;
	}
}
